﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace Directory.Web
{
    public partial class Controls_NavTree : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IList<Directory.Model.CategoryInfo> categories = CategoryDataProxy.GetChildsOfCategory(RootCategoryId);
            foreach (Directory.Model.CategoryInfo cat in categories)
            {
                TreeNode node = new TreeNode(cat.CategoryName, cat.CategoryId.ToString(), null, this.NavigationUrl + this.QueryKey + "=" + cat.CategoryId.ToString(), this.Target);
                this.NavTreeView.Nodes.Add(node);
            }
        }
        #region Properties

        private int _rootCategoryId = WebSettings.GetInstance().RootCategoryId;
        public int RootCategoryId
        {
            get { return _rootCategoryId; }
            set { _rootCategoryId = value; }
        }

        private string _target;
        public string Target
        {
            get { return _target; }
            set { _target = value; }
        }

        private string _navigationUrl;
        public string NavigationUrl
        {
            get { return _navigationUrl; }
            set { _navigationUrl = value; }
        }

        private string _queryKey = "categoryId";
        public string QueryKey
        {
            get { return _queryKey; }
            set { _queryKey = value; }
        }

        #endregion

        //TreeNode Expanded Event
        protected void NavTreeView_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count != 0)
            {
                IList<Directory.Model.CategoryInfo> childs = CategoryDataProxy.GetChildsOfCategory(int.Parse(e.Node.Value));
                foreach (Directory.Model.CategoryInfo cat in childs)
                {
                    TreeNode node = new TreeNode(cat.CategoryName, cat.CategoryId.ToString(), null, this.NavigationUrl + this.QueryKey + "=" + cat.CategoryId.ToString(), this.Target);
                    e.Node.ChildNodes.Add(node);
                }
            }
        }
    }
}
